import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiBody,
} from '@nestjs/swagger';
import { TicketService } from './ticket.service';
import type { Ticket, UserTicket } from './ticket.service';

@ApiTags('门票管理')
@Controller('tickets')
export class TicketController {
  constructor(private readonly ticketService: TicketService) {}

  @ApiOperation({ summary: '获取门票列表' })
  @ApiResponse({ status: 200, description: '成功获取门票列表' })
  @Get()
  findAll(): Ticket[] {
    return this.ticketService.findAll();
  }

  @ApiOperation({ summary: '根据ID获取门票详情' })
  @ApiResponse({ status: 200, description: '成功获取门票详情' })
  @ApiResponse({ status: 404, description: '门票不存在' })
  @Get(':id')
  findOne(@Param('id') id: string): Ticket {
    const ticket = this.ticketService.findById(id);
    if (!ticket) {
      throw new Error('Ticket not found');
    }
    return ticket;
  }

  @ApiOperation({ summary: '创建门票' })
  @ApiResponse({ status: 201, description: '门票创建成功' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '门票数据' })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Body() ticketData: Omit<Ticket, 'id' | 'availableQuantity' | 'status' | 'createdAt' | 'updatedAt'>): Ticket {
    return this.ticketService.create(ticketData);
  }

  @ApiOperation({ summary: '更新门票' })
  @ApiResponse({ status: 200, description: '门票更新成功' })
  @ApiResponse({ status: 404, description: '门票不存在' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '门票更新数据' })
  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() ticketData: Partial<Omit<Ticket, 'id' | 'createdAt' | 'availableQuantity'>>,
  ): Ticket {
    const result = this.ticketService.update(id, ticketData);
    if (!result) {
      throw new Error('Ticket not found');
    }
    return result;
  }

  @ApiOperation({ summary: '删除门票' })
  @ApiResponse({ status: 204, description: '门票删除成功' })
  @ApiResponse({ status: 404, description: '门票不存在' })
  @ApiBearerAuth()
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  remove(@Param('id') id: string): void {
    const result = this.ticketService.delete(id);
    if (!result) {
      throw new Error('Ticket not found');
    }
  }

  @ApiOperation({ summary: '购买门票' })
  @ApiResponse({ status: 201, description: '门票购买成功' })
  @ApiResponse({ status: 400, description: '门票库存不足' })
  @ApiResponse({ status: 404, description: '门票不存在' })
  @ApiBearerAuth()
  @ApiBody({ 
    schema: { 
      type: 'object', 
      properties: { 
        userId: { type: 'string' },
        quantity: { type: 'number' }
      } 
    } 
  })
  @Post(':id/purchase')
  purchase(
    @Param('id') id: string,
    @Body('userId') userId: string,
    @Body('quantity') quantity: number = 1,
  ): UserTicket {
    const result = this.ticketService.purchaseTicket(userId, id, quantity);
    if (!result) {
      throw new Error('Failed to purchase ticket');
    }
    return result;
  }

  @ApiOperation({ summary: '获取用户的门票' })
  @ApiResponse({ status: 200, description: '成功获取用户门票' })
  @ApiBearerAuth()
  @Get('user/:userId')
  getUserTickets(@Param('userId') userId: string): UserTicket[] {
    return this.ticketService.getUserTickets(userId);
  }

  @ApiOperation({ summary: '获取门票的购买记录' })
  @ApiResponse({ status: 200, description: '成功获取购买记录' })
  @ApiBearerAuth()
  @Get(':id/purchases')
  getTicketPurchases(@Param('id') id: string): UserTicket[] {
    return this.ticketService.getTicketPurchases(id);
  }
}